package com.lkd.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.OrderQueryDTO;
import com.lkd.entity.Order;
import com.lkd.mapper.OrderMapper;
import com.lkd.report.Collect;
import com.lkd.report.OrderCollect;
import com.lkd.result.TableInfo;
import com.lkd.service.OrderService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Z.lx
 * @Date: 2025/8/22 23:57
 * @Description:
 **/
@Service
@AllArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    private final OrderMapper orderMapper;

    /**
     * 订单列表
     * @param orderQueryDTO
     * @return
     */
    public TableInfo<Order> list(OrderQueryDTO orderQueryDTO) {
        PageHelper.startPage(orderQueryDTO.getPageIndex(), orderQueryDTO.getPageSize());
        List<Order> orderList = orderMapper.selectOrderList(orderQueryDTO);
        Page<Order> page = (Page<Order>) orderList;
        return new TableInfo<>(orderQueryDTO.getPageIndex(), orderQueryDTO.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @Override
    public Map<String, Object> collect(Integer amountType, String start, String end) {
        //按日统计 amountType=1
        Map<String,Object> orderCollect = new HashMap<>();
        List<String> xAxis = new ArrayList<>();
        List<Integer> series = new ArrayList<>();
        if(amountType == 1){
            List<LocalDate> localDates = formatTime(start, end);
            for(LocalDate date : localDates){
                int i = orderMapper.orderCountPrice(date)==null?0:orderMapper.orderCountPrice(date);
                series.add(i/10000);
                xAxis.add(date.toString());
                orderCollect.put("xAxis", xAxis);
                orderCollect.put("series", series);
            }
        }
        //按月统计 amountType=2
        if(amountType == 2){
            List<LocalDate> localDates = formatMonth(start, end);
            for(LocalDate date : localDates){
                YearMonth yearMonth = YearMonth.from(date);
                LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
                series.add(orderMapper.orderCountPrice(lastDayOfMonth));
                xAxis.add(orderMapper.orderCount(start, end));
                orderCollect.put("xAxis", xAxis);
                orderCollect.put("series", series);
            }
        }
        return orderCollect;
    }
    public List<LocalDate> formatMonth(String start, String end){
        LocalDate startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd").toLocalDate();
        LocalDate endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd").toLocalDate();
        List<LocalDate> dataList = new ArrayList<>();
        dataList.add(startTime);
        while(!startTime.getMonth().equals(endTime.getMonth())){
            startTime = startTime.plusMonths(1);
            dataList.add(startTime);
        }
        return dataList;
    }

    public List<LocalDate> formatTime(String start, String end) {
        LocalDate startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd").toLocalDate();
        LocalDate endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd").toLocalDate();
        List<LocalDate> dataList = new ArrayList<>();
        dataList.add(startTime);
        //循环，日期+1，添加到dateList
        while (!startTime.isEqual(endTime)) {
            startTime = startTime.plusDays(1);
            dataList.add(startTime);
        }
        return dataList;
    }
}
